<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 消息管理
 * Class Consumer
 * @author heyu
 */
class Consumer extends MY_Controller {
    /**
     * @var ConsumerMessageModel
     */
    public $mMessage;

	public function __construct(){
		parent::__construct();
        $this->loadModel('ConsumerMessageModel', 'mMessage', 'consumer');
	}
	
	/**
	 * 首页
	 */
	public function index(){

        if ( ! adminAuthority('consumer-add', 'message')){
            redirect(getUrl('message/consumer/history'));
        }
	    $data = array(
	        'title' => '消息推送'
        );
		$this->view('message/consumer/index', $data, 'datatable,validation,uniform,ueditor');
	}

    /**
     * 获取用户列表
     */
    public function getConsumerList(){
        $this->loadModel('ConsumerUserModel', 'mConUser', 'consumer');
        $_GET['iSortCol_0'] = (int)get('iSortCol_0') == 5 ? 7 : 0;
        $data = $this->mConUser->adminGetList(2);
        foreach ($data['data'] as $k => $v){
            $img = isset($v['portrait']) ? getImage($v['portrait'], 'xs') : getDefaultPortrait();
            $data['aaData'][$k][] = '<input width="100%" type="checkbox" class="checkboxes" name="consumer_id[]" value="' . $v['id'] .'">';
            $data['aaData'][$k][] = '<img src="'.$img.'" class="portrait img-wh"/>';
            $data['aaData'][$k][] = $v['realname'];
            $data['aaData'][$k][] = $v['nickname'];
            $data['aaData'][$k][] = $v['mobile'];
            $data['aaData'][$k][] = isset($v['last_login_time']) ? dateFormat($v['last_login_time']) : '';
            $data['aaData'][$k][] = $this->adminStatus($v['status'], true);
            $data['aaData'][$k][] = '';
        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 保存推送
     */
    public function save(){
        //表单验证
        $vConf = array(
            array('type', '类型', 'required|in_list[1,2]'),
            array('title', '标题', 'required'),
            array('content', '简介', 'required'),
            array('content_detail', '详细内容', 'required'),
        );
        $this->formDataValidation($vConf, array('id' => get('id')));
        $type = (int)post('type');
        $title = post('title');
        $content = post('content');
        $data = array();
        $time = time();
        $jPushCode = array();
        if ($type == 1){
            $data[] = array(
                'consumer_id' => 0,
                'type' => 1,
                'title' => $title,
                'content' => $content,
   		'content_detail' => trim(post('content_detail')),
                'addtime' => $time,
            );
        }else if ($type == 2) {
            $id = post('consumer_ids');
//            $ids = (int)post('ids');
            $consumerId = explode(',', $id);
            if (empty($consumerId)) {
                $this->ajaxReturn(0, '请选择要推送的用户1');
            }
            $ids = array();
            foreach ($consumerId as $k => $v) {
                if (isset($v) && $v > 0) {
                    $data[] = array(
                        'consumer_id' => $v,
                        'type' => 2,
                        'title' => $title,
                        'content' => $content,
			            'content_detail' => trim(post('content_detail')),
                        'addtime' => $time,
                    );

                    $ids[] = $v;
                }
            }
            $this->loadModel('ConsumerUserModel', 'mConsumer', 'consumer');

            $consumer = $this->mConsumer->getInfoByIds($ids);
            foreach ($consumer as $c){
                $jPushCode[] = $c['jpush_code'];
            }
        }
        if (empty($data)){
            $this->ajaxReturn(0, '请选择要推送的用户2');
        }
        $tA = $this->mMessage->batchSave($data);
        if (!$tA){
            $this->ajaxReturn(0, '推送失败');
        }
        $type = $type == 1 ? 2 : 1;
        jpush($title, $content, $type, $jPushCode);
        $this->ajaxReturn(1, '推送成功');
    }

    /**
     * 推送历史记录
     */
    public function history(){
        $data = array(
            'type' => $this->mMessage->getType(),
            'title' => '推送历史'
        );
        $this->view('message/consumer/history', $data, 'datatable,datepicker');
    }

    /**
     * 获取推送历史记录
     */
    public function getMessageList(){
        $data = $this->mMessage->adminPcGetList();
        foreach ($data['data'] as $k => $v){
            $data['aaData'][$k][] = $this->mMessage->getType($v['type']);
            $data['aaData'][$k][] = $v['title'];
            $data['aaData'][$k][] = '<textarea readonly class="form-control remaks-item content textarea_height">'.$v['content'].'</textarea>';
            $data['aaData'][$k][] = dateFormat($v['addtime']);
            $data['aaData'][$k][] = $v['type'] == 1 ? '推送所有人' : '推送'.$v['total'].'人';
            if ($v['type'] == 1){
                $data['aaData'][$k][] = '';
            }else{
                $data['aaData'][$k][] = adminCreateBtn(array(
                    'label' => '明细',
                    'attr' => array(
                        'href' => 'javascript:;',
                        'class' => 'btn btn-xs green-stripe default detail-btn',
                        'time' => $v['addtime'],
                        'date_time' => dateFormat($v['addtime']),
                    ),
                    'tag' => 'a',
                    'permissions' => array(
                        'module' => 'message',
                        'key' => 'consumer-view'
                    )
                ));
            }

        }
        unset($data['data']);
        echo json_encode($data);
    }

    /**
     * 获取同一时间消息推送人
     */
    public function getConsumerDetail(){
        $data = $this->mMessage->getConsumerDetail();
        foreach ($data['data'] as $k => $v){
            $portrait = isset($v['portrait']) ? getImage($v['portrait'], 'xs') : getDefaultImg();
            $data['aaData'][$k][] = '<img width="100%" src="'.$portrait.'" class="portrait img-wh"/>';
            $data['aaData'][$k][] = $v['realname'];
            $data['aaData'][$k][] = $v['nickname'];
            $data['aaData'][$k][] = $v['mobile'];
            $data['aaData'][$k][] = '';
        }
        unset($data['data']);
        echo json_encode($data);
    }
}
